"""
    Demonstrates creating a simple module to be used by other applications
"""

def rgb(r, g, b):
    """
        Take an integer rgb color, returns an HTML color string
    """
    return "rgb(%i, %i, %i)" % (r, g, b)

def rgbf(r, g, b):
    """
        Takes a floating point rgb color, returns an HTML color string
    """
    r = int(r * 255.0)
    g = int(g * 255.0)
    b = int(b * 255.0)
    return "rgb(%i, %i, %i)" % (r, g, b)

class Svg:
    """
        Creates an SVG element for rendering graphics to
    """
    def __init__(self, parent, width, height):
        self.width = width
        self.height = height
        self.ns = "http://www.w3.org/2000/svg"
        self.element = document.createElementNS(self.ns, "svg")
        parent.appendChild(self.element)
        self.element.setAttribute("width", self.width)
        self.element.setAttribute("height", self.height)
        self.bg = document.createElementNS(self.ns, "rect")
        self.bg.setAttribute("width", self.width)
        self.bg.setAttribute("height", self.height)
        self.element.appendChild(self.bg)
        self.g = document.createElementNS(self.ns, "g")
        self.element.appendChild(self.g)

    def smoothEdges(self, on):
        """
            Turning this off will speed up rendering by a little
        """
        if on:
            self.element.setAttribute("shape-rendering", "auto")
        else:
            self.element.setAttribute("shape-rendering", "optimizeSpeed")

    def appendChild(self, child):
        self.g.appendChild(child)

    def bgColor(self, r, g, b):
        self.bg.setAttribute("fill", rgb(r, g, b))

